<?php
// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
//        used to display a nice indented tree
function display_children( ) {
	$xo = $level+1;
   // retrieve all children of $parent
   $result = my_query( "SELECT * FROM jabatan WHERE nama_jabatan <> '-' AND parent_struktur_id ='0'" );
   // display each child
   $view ='';
   while ($row = my_fetch_array($result)) {
		$editproperty = array(
						'href'=>'index.php?com=struktur_organisasi&task=edit&id='.$row['id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$row['id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$view .= '<li><span class="folder">'. ucfirst(strtolower($row['nama_jabatan'])) .'</span>';
		
		

			$result2 = my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_struktur_id='{$row['jabatan_id']}'" );
			if(my_num_rows($result2) > 0){
			$view .= '<ul>';
			while($row2 = my_fetch_array($result2)){
				$view .= '<li><span class="folder">'. ucfirst(strtolower(($row2['nama_jabatan'] <> '-' ? $row2['nama_jabatan'] : '__________'))) .'</span>'; 
		
				$res3 =  my_query( "SELECT * FROM jabatan WHERE nama_jabatan <> '-' AND parent_struktur_id='{$row2['jabatan_id']}'" );
				if(my_num_rows($res3) > 0){
				$view .= '<ul>';
				while($row3 = my_fetch_array($res3)){
					$view .= '<li><span class="folder">'. ucfirst(strtolower( ($row3['nama_jabatan'] <> '-' ? $row3['nama_jabatan'] : '__________') )) .'</span>';
					
					$result4 = my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_struktur_id='{$row3['jabatan_id']}'" );
					if(my_num_rows($result4) > 0){
					$view .= '<ul>';
					while($row4 = my_fetch_array($result4)){
						$view .= '<li><span class="folder">'. ucfirst(strtolower( ($row4['nama_jabatan'] <> '-' ? $row4['nama_jabatan'] : '__________') )) .'</span>';
					
						$result5 = my_query( "SELECT * FROM jabatan WHERE  nama_jabatan <> '-' AND  parent_struktur_id='{$row4['jabatan_id']}'" );
						if(my_num_rows($result5) > 0){
						$view .= '<ul>';
						while($row5 = my_fetch_array($result5)){
							$view .= '<li><span class="folder">'. ucfirst(strtolower( ($row5['nama_jabatan'] <> '-' ? $row5['nama_jabatan'] : '__________') )) .'</span>';
					
					
						}
						$view .= '<ul>';
						}
					}
					$view .= '<ul>';
					}
				}
				$view .= '</ul>';
				}
			}
			$view .= '</ul>';
			}
   } 
   return $view;
} 

function has_child( $jabatan_id ){
	$result = my_query("SELECT * FROM   jabatan WHERE nama_jabatan <> '-' AND parent_id='{$jabatan_id}'");
	if(my_num_rows($result) > 0){
		return true;
	}
	return false;
}

function has_parent($jabatan_id  ){
	$result = my_query("SELECT * FROM   jabatan WHERE nama_jabatan <> '-' AND parent_id='{$jabatan_id}'");
	if(my_num_rows($result) > 0){
		return true;
	}
	return false;
}

function struktur_organisasi(){
//PASANG JAVASCRIPT
my_set_file_js( 
	array(
		'components/system/jquery/jquery.treeview/lib/jquery.cookie.js',
		'components/system/jquery/jquery.treeview/jquery.treeview.js',
		'components/system/jquery/jquery.treeview/demo/demo.js',
	) 
 );
my_set_file_css(
	array('components/system/jquery/jquery.treeview/jquery.treeview.css')
);
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com=struktur_organisasi&task=delete&id=\'+id;
	}
	return false;
}

'); 
return '<ul id="browser" class="filetree">
'.display_children( ) .'
</ul>';
}

function form_so_submit($id){
	$isasis = isset($_POST['is_assisten']) ? 'yes':'no';
	$datas = array( 
		'parent_id'=>my_type_data_str( $_POST['parent_id'] ),
		'is_asisten'	=>my_type_data_str( $isasis),
		'datetime_added'=>my_type_data_function('NOW()'),
	);
	if($id > 0){
		my_update_record('struktur_organisasi' ,'id' , $id , $datas);
	}else{
		$id=my_insert_record('struktur_organisasi' , $datas);
	} ;
	return update_jabatan($id,$_POST['nama_jabatan'])	;
}

function form_struktur_organisasi_create($id = 0){
	$view = form_header( "devisi" , "devisi"  );
 	$fields = my_get_data_by_id('struktur_organisasi','id',$id);
 	$jabatan = my_get_data_by_id('jabatan','struktur_organisasi_id',$id);

	$nama_jabatan = array(
				'name'=>'nama_jabatan',
				'value'=>( isset($_POST['nama_jabatan']) ? $_POST['nama_jabatan'] : $jabatan['nama_jabatan'] ),
				'id'=>'nama_jabatan',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama_jabatan);
	$view .= form_field_display( $form_field_nama   , "Nama Jabatan"   );

	$form_field ='<select name="parent_id">'.data_form_display_children($fields['parent_id']  ).'</select>';
	$view .= form_field_display( $form_field    , "Parent"   );
	
	$view .= form_field_display('<input type="checkbox" name="is_assisten" value="1"/>' ,'Asisten');
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

function data_form_display_children($id ){
ob_start();
form_display_children(0, 0 , $id);
$content = ob_get_clean(); 
return '<option value="0"></option>'.$content;
}

function form_display_children($parent, $level ,$id) {
   // retrieve all children of $parent
   $result = my_query('SELECT a.id,b.nama_jabatan FROM struktur_organisasi a '.
						'inner join jabatan b on a.id=b.struktur_organisasi_id '.
                        'WHERE a.parent_id="'.$parent.'";');
						  
   // display each child
   
   while ($row = my_fetch_array($result)) { 
       // indent and display the title of this childse
	   if($id == $row['id'])
	   echo    '<option value="'.$row['id'].'" selected>'.str_repeat('-----',$level) .' '.$row['nama_jabatan'].' </option>'  ."\n" ;
		
	   else
       echo    '<option value="'.$row['id'].'">'.str_repeat('-----',$level) .' '.$row['nama_jabatan'].' </option>'  ."\n" ;
				

       // call this function again to display this
       // child's children
       form_display_children($row['id'], $level+1 , $id);
   } 
}

function form_struktur_organisasi_validate(){
	$errsubmit = false;
	$err = array();
	 
	/*CHECK CODE*/
/*
	$nama =  trim($_POST["nama"]); 
	$data_code = my_result("SELECT id FROM struktur_organisasi WHERE nama_jabatan='{$nama}' "); 
	$exists_code = ( $data_code == '' ) ? false : true ;  
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama organisasi belum di isi";
	}
	elseif($exists_code){
		$errsubmit = true;
		$err[] = "Kode organisasi sudah digunakan";	
	}
 */   
	/*CHECK PARENT ID*/
	$parent_id = trim($_POST["parent_id"]);
	if($parent_id == '0'){
		$errsubmit = true;
		$err[] = "Parent struktur belum di pilih";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function remove_struktur_organisasi($id){
	$query= "DELETE FROM jabatan WHERE struktur_organisasi_id = {$id}";
	my_query($query);
	$query= "DELETE FROM struktur_organisasi WHERE id = {$id}";
	return my_query($query);
}   

function update_jabatan($id,$nama){
	$fields = my_get_data_by_id( 
		'jabatan' , 
		'struktur_organisasi_id' , 
		$id );
	$datas = array(
		'struktur_organisasi_id'=>my_type_data_int($id),
		'nama_jabatan'=>my_type_data_str($nama),
	);
	if($fields){
			return my_update_record('jabatan' , 'struktur_organisasi_id' , $id , $datas );
	}
	return my_insert_record( 'jabatan' , $datas);
}
